namespace Digitalmes.Common.Workflows;

/// <summary>
/// 工作流步骤节点构建器基类。
/// </summary>
/// <param name="name">流程名称。</param>
public abstract class WorkflowStepBuilder(string name)
{
    /// <summary>
    /// 节点唯一 Id。
    /// </summary>
    public string Id { get; } = Guid.NewGuid().ToString("n");

    /// <summary>
    /// 节点唯一名称。
    /// </summary>
    public string Name { get; } = name;

    /// <summary>
    /// 获取边缘连线集合。
    /// </summary>
    internal Dictionary<string, List<WorkflowEdgeBuilder>> Edges { get; } = [];

    /// <summary>
    /// 步骤节点中要触发的事件。
    /// </summary>
    /// <param name="eventId">要触发的事件名称。</param>
    /// <returns></returns>
    public WorkflowEdgeBuilder OnEvent(string eventId)
    {
        return new WorkflowEdgeBuilder(this, eventId);
    }

    /// <summary>
    /// 此步骤连接到边缘。
    /// </summary>
    /// <param name="eventId"></param>
    /// <param name="edgeBuilder"></param>
    internal virtual void LinkTo(string eventId, WorkflowEdgeBuilder edgeBuilder)
    {
        if (!Edges.TryGetValue(eventId, out var edges) || edges == null)
        {
            edges = [];
            Edges[eventId] = edges;
        }

        edges.Add(edgeBuilder);
    }

    /// <summary>
    /// 
    /// </summary>
    /// <param name="functionName"></param>
    /// <param name="parameterName"></param>
    /// <returns></returns>
    /// <exception cref="NotImplementedException"></exception>
    internal virtual WorkflowFunctionTarget ResolveFunctionTarget(string? functionName, string? parameterName)
    {
        //string? verifiedFunctionName = functionName;
        //string? verifiedParameterName = parameterName;
        throw new NotImplementedException();
    }
}

public sealed class WorkflowStepBuilder<TStep> : WorkflowStepBuilder where TStep : WorkflowStep
{
    internal WorkflowStepBuilder(string? name = null)
       : base(name ?? typeof(TStep).Name)
    {
    }
}
